package com.shareach.common;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.ReflectionUtils.FieldFilter;

/**
 * 
 * @author zuoshuaiok@gmail.com
 * 
 * @param <T>
 */
public class ListPropertyAndValueTool {


	public static <T> Map<String, Object> display(final T t, final List<String> fieldNames) {

		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			for (String name : fieldNames) {

				Method m = t.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
				String value = m.invoke(t).toString();
				if(m.getReturnType().toString().equals("class java.util.Date")){
					
					value = sdf.format(sdf.parse(value));
				} 
				map.put(name, value);
			}
			
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 
	 * @param query
	 *            mongodb 查询对象
	 * @param order
	 *            请求查询表单
	 * @param document
	 *            mongodb 文档对象
	 * @param exincludeField
	 *            排除的属
	 */
	public static <T, V> void querySaveCriteria(final Query query,
			final T order, final Class<V> document,
			final List<String> includeField) {

		ReflectionUtils.doWithFields(order.getClass(), new FieldCallback() {
			@Override
			public void doWith(Field field) throws IllegalArgumentException,
					IllegalAccessException {

				ReflectionUtils.makeAccessible(field);
				Object obj = ReflectionUtils.getField(field, order);
				if (obj == null) {
					return;
				}
				//  if(obj.getClass().isArray() ,obj instanceof List obj
				// instanceof Set obj instanceof Map)
				if (obj instanceof String && StringUtils.isEmpty((String) obj)) {
					return;
				}

				//query.addCriteria(new Criteria(field.getName()).is(obj));
				query.addCriteria(new Criteria(field.getName()).regex(".*?"+obj+".*"));

			}
		}, new FieldFilter() {
			@Override
			public boolean matches(Field field) {

				if (CollectionUtils.containsInstance(includeField,
						field.getName())
						&& null != ReflectionUtils.findField(document,
								field.getName())) {
					return true;
				}
				return false;
			}
		});

	}

}
